import base64
import json
from flask import Blueprint, render_template, redirect, make_response, jsonify, session
from flask import request
# from app import app
from development import SECRETKEY
import requests
import development as dp
from flask_jwt_extended import create_access_token,jwt_required, get_jwt_identity,verify_jwt_in_request

bp = Blueprint("cal", __name__, url_prefix='/cal')
from mongoDb_connection.connection import initDatabase, addData, find_data, update_data

@bp.route('/modelCal', methods=['GET', 'POST'])
@jwt_required()
def modelcal():
    print(request.json)
    current_user = get_jwt_identity()
    print(current_user)
    db = initDatabase(dp.DATABASE_NAME)
    # 计算
    #
    add_json = {
        'user': current_user,
        "model_path": '../w',
        "model_name": request.json['model_name'],
        "loss_arr": [2.342,1.3, 1.1, 0.5, 0.3, 0.1, 0.08, 0.07, 0.04,0.03],
        "basic_info": request.json
    }
    res_add = addData(db, add_json, 'modelCal')
    if res_add['res'] == 'fail':
        return {"msg": 'database error', "code": 500}, 500
    return {"msg": 'add success', "code": 200, "id": res_add['id_']}, 200